/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import domain.Sex;
import domain.Student;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import model.EltcException;
import model.Model;
import model.ModelImpl;

/**
 *
 * @author user
 */
public class FabricaBeanCreate {

  private static  Model model = new ModelImpl();

    public static  Object createBean(Class beanClass, HttpServletRequest request) throws EltcException {
        Object beanToReturn = null;
        if (beanClass.equals(Student.class)) {
            beanToReturn = getForSetStudent(request);
        }

        return beanToReturn;
    }

    private static  Student getForSetStudent(HttpServletRequest request) throws EltcException {
        Student student = new Student();
        student.setId(Integer.valueOf(request.getParameter("id")));
        Sex sex = null;
        System.out.println("request.getParameter(\"sex\")="+request.getParameter("sex"));
        if (request.getParameter("sex") != null && request.getParameter("sex").equals("1")) {
            sex = model.getObject(1, Sex.class);
        } else if (request.getParameter("sex") != null && request.getParameter("sex").equals("2")) {
            sex = model.getObject(2, Sex.class);
        } else {
            throw new EltcException("Секс не установлен из базы данных!");
        }
        student.setSex(sex);
        student.setLastNameRu(request.getParameter("lastNameRu"));
        student.setFirstNameRu(request.getParameter("firstNameRu"));
        student.setPatronicNameRu(request.getParameter("patronicNameRu"));
        student.setLastNameEn(request.getParameter("lastNameEn"));
        student.setFirstNameEn(request.getParameter("firstNameEn"));
        student.setPatronicNameEn(request.getParameter("patronicNameEn"));
        Date dateOfBirth = ServletUtilMethods.getDateFromString(request.getParameter("dateOfBirth"));
        student.setDateOfBirth(dateOfBirth);
        student.setEmailOffice(request.getParameter("emailOffice"));
        student.setEmailHome(request.getParameter("emailHome"));
        student.setEmailAdd(request.getParameter("emailAdd"));
        student.setPhoneHome(request.getParameter("phoneHome"));
        student.setPhoneMobile1(request.getParameter("phoneMobile1"));
        student.setPhoneMobile2(request.getParameter("phoneMobile2"));
        student.setPhoneOffice(request.getParameter("phoneOffice"));
        student.setSkype(request.getParameter("skype"));
        student.setIcq(request.getParameter("icq"));
        student.setMailruAgent(request.getParameter("mailruAgent"));
        student.setComments(request.getParameter("comments"));
        student.setPhoto(request.getParameter("photo"));
        student.setPersonVueIdent(request.getParameter("personVueIdent"));
        student.setPrometricIdent(request.getParameter("prometricIdent"));
        student.setOracleLogin(request.getParameter("oracleLogin"));
        student.setMicrosoftLogin(request.getParameter("microsoftLogin"));
        student.setUser(request.getParameter("user"));
        student.setInsertDatetime(new Date());
        return student;
    }
}
